package com.example.bobo.yuejian322.Activity;

import android.Manifest;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bobo.yuejian322.Interfa.PeixunSeverice;
import com.example.bobo.yuejian322.MyImage.ConsultDialog;
import com.example.bobo.yuejian322.R;
import com.example.bobo.yuejian322.base.BaseActivity;
import com.example.bobo.yuejian322.bean.TrainDetial;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static sun.rmi.transport.TransportConstants.Call;

public class PeixunDetialActivity extends BaseActivity {
    private String id;
    private ImageView IV;
    private TextView Jianjie,Xuzhi,Time,Place,Price,button_zhixun,button_baoming,TrainName;
    private TextView PhoneNumber;
    private String phone;
    private ImageButton back;
     private String    peixunname;
     private  int    BMPrice;
    private String bmnotice;
    private String userid;
    private  String Train_id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_peixun_detial);

        id = getIntent().getStringExtra("peixunID");

        PhoneNumber= (TextView) findViewById(R.id.PhoneNumber);
        IV= (ImageView) findViewById(R.id.image_detialPeixun);
        Jianjie= (TextView) findViewById(R.id.jianjie);
        Xuzhi= (TextView) findViewById(R.id.shangkexuzhi);
        Time= (TextView) findViewById(R.id.shoukeshijian);
        Place= (TextView) findViewById(R.id.shangkedidian);
        Price= (TextView) findViewById(R.id.jiage);
        button_zhixun= (TextView) findViewById(R.id.lijizhixun);
        button_baoming= (TextView) findViewById(R.id.lijibaoming);
        TrainName= (TextView) findViewById(R.id.train_name);
        back= (ImageButton) findViewById(R.id.peixun_detial_back);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175/workout/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        PeixunSeverice peixunSeverice =retrofit.create(PeixunSeverice.class);
        Call<TrainDetial> listCall = peixunSeverice.traindetial(userId,id,"train");

        listCall.enqueue(new Callback<TrainDetial>() {
            @Override
            public void onResponse(Call<TrainDetial> call, Response<TrainDetial> response) {
                TrainDetial  traindetial = response.body();

                if (PeixunDetialActivity.this != null) {

                    if (traindetial.getThumb() != "") {
                        Picasso.with(PeixunDetialActivity.this).load("http://172.24.10.175/workout/Uploads/"+traindetial.getThumb()).
                                into(IV);
                    }
                    phone=traindetial.getTel();
                    peixunname=traindetial.getName();
                    BMPrice=traindetial.getPrice();
                    bmnotice=traindetial.getBmnotice();
                    userid=traindetial.getUserid();
                    Train_id=traindetial.getId();


                    TrainName.setText(traindetial.getName());
                    Jianjie.setText(traindetial.getIntro());
                     Xuzhi.setText(traindetial.getNotice());
                    Time.setText(traindetial.getTraintime());
                    Place.setText(traindetial.getPlace());
                    Price.setText("￥"+traindetial.getPrice());
                    //PhoneNumber.setText(phone);

                }

            }

            @Override
            public void onFailure(Call<TrainDetial> call, Throwable t) {

            }
        });

        //返回箭头
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();

            }
        });

//咨询


        button_zhixun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager fm = getFragmentManager();
                ConsultDialog dialog = new ConsultDialog(PeixunDetialActivity.this,R.layout.dialog_consult);
                dialog.show(fm, "showPhotoSelDialog");


            }
        });

//报名
        button_baoming.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            Intent intent=new Intent();
                intent.setClass(getApplication(),BaomingActivity.class);


                intent.putExtra("peixunmane", peixunname);
                intent.putExtra("bmPrice", BMPrice);
                intent.putExtra("bmnotice", bmnotice);
                intent.putExtra("userid", userid);
                intent.putExtra("Train_id", Train_id);



                startActivity(intent);
            }
        });






    }

    public void callPhone(){

        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
        if (ActivityCompat.checkSelfPermission(PeixunDetialActivity.this,
                Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        startActivity(intent);



    }



}
